home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1996 June: Reference Library / Dev.CD Jun 96 RL / Dev.CD Jun 96 RL.toast / What's New? / Development Kits / Apple Game Sprockets DR1 / Examples / InputSprocketTest / InputSprocketSimpleTest.cp
Encoding:
Text File  |  1996-04-24  |  9.0 KB  |  428 lines  |  [TEXT/MPS ]

  1. #include <stdio.h>
  2. #include "InputSprocket.h"
  3. #include "SIOUX.h"
  4.  
  5. void ShowFourByte(OSType fourByte);
  6. void ShowFourByte(OSType fourByte)
  7. {
  8.     putchar(((fourByte & 0xff000000) >> 24));
  9.     putchar(((fourByte & 0x00ff0000) >> 16));
  10.     putchar(((fourByte & 0x0000ff00) >> 8));
  11.     putchar(((fourByte & 0x000000ff) >> 00));
  12. }
  13.  
  14. void ShowStr63(const Str63 &theStr);
  15. void ShowStr63(const Str63 &theStr)
  16. {
  17.     int i;
  18.     
  19.     for(i=1; i <= theStr[0]; i++)
  20.     {
  21.         putchar(theStr[i]);
  22.     }
  23. }
  24.  
  25.  
  26. void PrintEvent(ElementEventPtr theEvent);
  27. void PrintEvent(ElementEventPtr theEvent)
  28. {
  29.     printf("when = [%lu] [%lu]\n", theEvent->when.hi, theEvent->when.lo);
  30.     printf("data = %ld\n", theEvent->data);
  31.     printf("element = %lx\n", theEvent->element);
  32.     printf("refcon = %lx\n", theEvent->refCon);
  33.     
  34.     ElementInfo info;
  35.     GetElementInfo(theEvent->element, &info);
  36.     
  37.     printf("label = ");
  38.     ShowFourByte(info.theLabel);
  39.     printf("\n");
  40.     
  41.     printf("kind = ");
  42.     ShowFourByte(info.theKind);
  43.     printf("\n");
  44.     
  45.     printf("string = ");
  46.     ShowStr63(info.theString);
  47.     printf("\n");
  48.     
  49.     printf("\n\n");
  50. }
  51.  
  52. OSErr TwoButtonJoystick(UInt32 &status, ElementReference &lastXAxis, ElementReference &lastYAxis, 
  53.                         SInt32 &xAxis, SInt32 &yAxis, SInt32 &POVHat, Boolean &button1, Boolean &button2);
  54. OSErr TwoButtonJoystick(UInt32 &status, ElementReference &lastXAxis, ElementReference &lastYAxis, 
  55.                         SInt32 &xAxis, SInt32 &yAxis, SInt32 &POVHat, Boolean &button1, Boolean &button2)
  56. {
  57.     enum
  58.     {
  59.         kButton1ShouldBe0 = 0x00000001,
  60.         kButton2ShouldBe0 = 0x00000002
  61.     };
  62.     
  63.     if (status & kButton1ShouldBe0)
  64.     {
  65.         button1 = false;
  66.     }
  67.     
  68.     if (status & kButton2ShouldBe0)
  69.     {
  70.         button2 = false;
  71.     }
  72.  
  73.     status = 0;
  74.     
  75.     Boolean button1MustBeDown = false;
  76.     Boolean button2MustBeDown = false;
  77.     
  78.     int maxEvents = 10;            // only do events ten at a time, they might be continuously streaming out
  79.     ElementEvent theEvent;
  80.     Boolean wasEvent = true;
  81.     OSErr err = noErr;
  82.     ElementListReference globalList;
  83.     
  84.     err = GetGlobalElementList(&globalList);
  85.  
  86.     while(maxEvents > 0)
  87.     {    
  88.         err = ElementListGetNextEvent(globalList, sizeof(theEvent), &theEvent, &wasEvent);
  89.         
  90.         if ((wasEvent == false) || (err))
  91.         {
  92.             break;
  93.         }
  94.         
  95.         // get information about this element since we are not doing any configuration
  96.         // we need to know the about labels and kinds
  97.         ElementInfo info;
  98.         GetElementInfo(theEvent.element, &info);
  99.  
  100.         switch(info.theKind)
  101.         {
  102.             case kElementKindButton:
  103.             {
  104.                 ButtonConfigurationInfo buttonConfig;
  105.                 Boolean newValue;
  106.                 
  107.                 // figure out the new value for the boolean
  108.                 if (theEvent.data)
  109.                 {
  110.                     newValue = true;
  111.                 }
  112.                 else
  113.                 {
  114.                     newValue = false;
  115.                 }
  116.     
  117.                 // need configuration information to determine if this is button1 or not
  118.                 GetElementConfigurationInfo(theEvent.element, sizeof(buttonConfig), &buttonConfig);
  119.                                 
  120.                 // we will call button id 1 our buttons 1, usually the trigger
  121.                 if (buttonConfig.id == 1)
  122.                 {
  123.                     if (newValue)
  124.                     {
  125.                         button1MustBeDown = true;
  126.                     }
  127.  
  128.                     button1 = newValue;
  129.                 }
  130.                 else
  131.                 {
  132.                     button2 = newValue;
  133.                     
  134.                     if (newValue)
  135.                     {
  136.                         button2MustBeDown = true;
  137.                     }
  138.                 }
  139.             }
  140.             break;
  141.             
  142.             case kElementKindDPad:
  143.             {
  144.                 if (info.theLabel == kElementLabelPOVHat)
  145.                 {
  146.                     POVHat = theEvent.data;
  147.                 }
  148.                 else if (info.theLabel == kElementLabelPadMove)
  149.                 {
  150.                     // a movement DPad so they might be using
  151.                     // a console style direction pad so turn
  152.                     // that into axis style data
  153.                     
  154.                     lastXAxis = nil;
  155.                     lastYAxis = nil;
  156.                     
  157.                     switch(theEvent.data)
  158.                     {
  159.                         case kPadIdle:
  160.                             xAxis = kAxisCenter;
  161.                             yAxis = kAxisCenter;
  162.                             break;
  163.                         case kPadLeft:
  164.                             xAxis = kAxisMinimum;
  165.                             yAxis = kAxisCenter;
  166.                             break;
  167.                         case kPadUpLeft:
  168.                             xAxis = kAxisMinimum;
  169.                             yAxis = kAxisMaximum;
  170.                             break;
  171.                         case kPadUp:
  172.                             xAxis = kAxisCenter;
  173.                             yAxis = kAxisMaximum;
  174.                             break;
  175.                         case kPadUpRight:
  176.                             xAxis = kAxisMaximum;
  177.                             yAxis = kAxisMaximum;
  178.                             break;
  179.                         case kPadRight:
  180.                             xAxis = kAxisMaximum;
  181.                             yAxis = kAxisCenter;
  182.                             break;
  183.                         case kPadDownRight:
  184.                             xAxis = kAxisMaximum;
  185.                             yAxis = kAxisMinimum;
  186.                             break;
  187.                         case kPadDown:
  188.                             xAxis = kAxisCenter;
  189.                             yAxis = kAxisMinimum;
  190.                             break;
  191.                         case kPadDownLeft:
  192.                             xAxis = kAxisMinimum;
  193.                             yAxis = kAxisMinimum;
  194.                     }
  195.                 }
  196.             }
  197.             break;
  198.             
  199.             case kElementKindAxis:
  200.             {
  201.                 // if it is an axis find out if it is the
  202.                 // x or y axis style data and use that.
  203.                 
  204.                 if (info.theLabel == kElementLabelXAxis)
  205.                 {
  206.                     lastXAxis = theEvent.element;
  207.                     
  208.                     xAxis = theEvent.data;
  209.                 }
  210.                 else if (info.theLabel == kElementLabelYAxis)
  211.                 {
  212.                     lastYAxis = theEvent.element;
  213.                     
  214.                     yAxis = theEvent.data;
  215.                 }
  216.             }
  217.             break;
  218.         }
  219.         
  220.         maxEvents--;
  221.     }
  222.     
  223.     if (button1MustBeDown && !(button1))
  224.     {
  225.         button1 = true;
  226.         
  227.         status |= kButton1ShouldBe0;
  228.     }
  229.     
  230.     if (button2MustBeDown && !(button2))
  231.     {
  232.         button2 = true;
  233.         
  234.         status |= kButton2ShouldBe0;
  235.     }
  236.     
  237.     // poll the last meaningful axis we had on the way out
  238.     // just to make sure we had good data
  239.     if (lastXAxis != nil)
  240.     {
  241.         GetElementSimpleState(lastXAxis, &xAxis);
  242.     }
  243.     
  244.     if (lastYAxis != nil)
  245.     {
  246.         GetElementSimpleState(lastYAxis, &yAxis);
  247.     }
  248.     
  249.     return err;
  250. }
  251.  
  252. ElementReference gLocalButton = nil;
  253.  
  254. void CreateLocalButton(void);
  255. void CreateLocalButton(void)
  256. {
  257.     return;
  258.     
  259.     ButtonConfigurationInfo thisButtonConfiguration;
  260.     thisButtonConfiguration.id = 1;
  261.  
  262.     ElementDefinitionStruct localButtonConfig;
  263.  
  264.     localButtonConfig.group = 0;
  265.     localButtonConfig.device = nil;
  266.     Str63 localButtonConfigString = "\pLocal Button";
  267.     ::BlockMove(&localButtonConfigString, &localButtonConfig.theString, localButtonConfigString[0] + 1);
  268.     localButtonConfig.configInfo = &thisButtonConfiguration;
  269.     localButtonConfig.configInfoLength = sizeof(thisButtonConfiguration);
  270.     localButtonConfig.kind = kElementKindButton;
  271.     localButtonConfig.label = 'none';
  272.     localButtonConfig.dataSize = 4;
  273.  
  274.     NewElement(&localButtonConfig, &gLocalButton);
  275. }
  276.  
  277. void PushLocalButton(void);
  278. void PushLocalButton(void)
  279. {
  280.     return;
  281.     
  282.     static SInt32 lastPush = 0;
  283.     AbsoluteTime time;
  284.     
  285.     time.hi = 0;
  286.     time.lo = 0;
  287.  
  288.     if (lastPush == 0)
  289.     {
  290.         lastPush = 1;
  291.     }
  292.     else
  293.     {
  294.         lastPush = 0;
  295.     }
  296.     
  297.     ElementPushSimpleData(gLocalButton, lastPush, &time);
  298. }
  299.  
  300.  
  301. void PrintElementBlock(ElementReference *theElementReferences, UInt32 count);
  302. void PrintElementBlock(ElementReference *theElementReferences, UInt32 count)
  303. {
  304.     printf("count = %d\n");
  305.  
  306.     int itr;
  307.     
  308.     for(itr = 0; itr < count; itr++)
  309.     {
  310.         printf("element #%d\n",itr);
  311.         
  312.         ElementInfo info;
  313.  
  314.         GetElementInfo(theElementReferences[itr], &info);
  315.  
  316.         printf("label = ");
  317.         ShowFourByte(info.theLabel);
  318.         printf("\n");
  319.  
  320.         printf("kind = ");
  321.         ShowFourByte(info.theKind);
  322.         printf("\n");
  323.  
  324.         printf("string = ");
  325.         ShowStr63(info.theString);
  326.         printf("\n\n");
  327.     }
  328.     
  329.     printf("\n");
  330. }
  331.  
  332. void main(void)
  333. {
  334.     ElementReference lastXAxis = nil;
  335.     ElementReference lastYAxis = nil;
  336.     SInt32 xAxis = 0;
  337.     SInt32 yAxis = 0;
  338.     SInt32 POVHat = 0;
  339.     Boolean button1 = 0;
  340.     Boolean button2 = 0;
  341.     UInt32 status = 0;
  342.     
  343.     printf("starting up...\n");
  344.     CreateLocalButton();
  345.  
  346.     
  347.     // printf("msr = %lx\n",GetMSR());
  348.     
  349.     ElementListReference globalList;
  350.     OSErr err;
  351.     
  352.     err = GetGlobalElementList(&globalList);
  353.     if (err)
  354.     {
  355.         printf("error getting global list (%ld)\n",err);
  356.     }
  357.     
  358.     UInt32 bufferSize = 5;
  359.     ElementReference theElementReferences[200];
  360.     UInt32 count;
  361.  
  362.     printf("extracting the elements!\n");    
  363.     ElementListExtract(globalList, bufferSize, &count, theElementReferences);
  364.     PrintElementBlock(theElementReferences, count);
  365.  
  366.     printf("kind = trigger\n");    
  367.     ElementListExtractByKind(globalList, kElementKindTrigger, bufferSize, &count, theElementReferences);
  368.     PrintElementBlock(theElementReferences, count);
  369.  
  370.     printf("kind = button\n");    
  371.     ElementListExtractByKind(globalList, kElementKindButton, bufferSize, &count, theElementReferences);
  372.     PrintElementBlock(theElementReferences, count);
  373.  
  374.     printf("kind = direction pad\n");    
  375.     ElementListExtractByKind(globalList, kElementKindDPad, bufferSize, &count, theElementReferences);
  376.     PrintElementBlock(theElementReferences, count);
  377.  
  378.     printf("kind = axis\n");    
  379.     ElementListExtractByKind(globalList, kElementKindAxis, bufferSize, &count, theElementReferences);
  380.     PrintElementBlock(theElementReferences, count);
  381.  
  382.     printf("label = xaxis\n");
  383.     ElementListExtractByLabel(globalList, kElementLabelXAxis, bufferSize, &count, theElementReferences);
  384.     PrintElementBlock(theElementReferences, count);
  385.  
  386.     while(1)
  387.     {
  388. #if 1
  389.         ElementEvent event;
  390.         Boolean wasEvent;
  391.  
  392.         ElementListGetNextEvent(globalList, sizeof(event), &event, &wasEvent);
  393.  
  394.         if (wasEvent)
  395.         {
  396.             PrintEvent(&event);
  397.         }
  398.         
  399. #else 
  400.  
  401.         PushLocalButton();
  402.         TwoButtonJoystick(status, lastXAxis, lastYAxis, xAxis, yAxis, POVHat, button1, button2);
  403.         printf("xAxis = %ld\n",xAxis);
  404.         printf("yAxis = %ld\n",yAxis);
  405.         printf("POVHat = %ld\n",POVHat);
  406.         printf("Button 1 = %d\n",button1);
  407.         printf("Button 2 = %d\n",button2);
  408.         
  409.         printf("return\n");
  410.         char foo[255];
  411.         scanf("%s\n",foo);
  412.  
  413. #endif
  414.         KeyMap theKeys;
  415.         GetKeys(theKeys);
  416.  
  417.         if ((theKeys[1] & 0x8000) && (theKeys[1] & 0x800000))
  418.         {
  419.             break;
  420.         }
  421.         
  422.         SIOUXHandleOneEvent(nil);
  423.       }
  424.      
  425.      
  426.      printf("later\n");
  427. }
  428.